<?php

defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'detail';
$return=array();
$uniacid = $_SESSION['uniacid'];
$mid = $_SESSION['uid'];
$basic=$this->plugin_setting('basic');
$return['basic']=$basic;
if($_RQ['op']=='detail'){
	/*******main*******/
	//团详情
	$orderid = intval($_RQ['orderid']);
	$teamid = intval($_RQ['teamid'])?:$_RQ['id'];
	if(!$orderid && !$teamid){
		exi('该团不存在!','error');
	}
	$team=pdo_get('jy_aligroup_groups_team',array('uniacid'=>$_SESSION['uniacid'],'id'=>$teamid));
	$myorder=pdo_getcolumn('jy_aligroup_groups_order',array('uniacid'=>$_SESSION['uniacid'],'teamid'=>$teamid,'mid'=>$_SESSION['uid']),'id');
	$group=pdo_get('jy_aligroup_groups',array('uniacid'=>$_SESSION['uniacid'],'id'=>$team['groupid']));
	$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$team['goodsid']));
	$team['num']=pdo_count('jy_aligroup_groups_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'teamid'=>$team['id']));
	$n=$group['groupnum']-$team['num'];
	//商品拼团价
	if($group['agio']!=0){
		$goods['groupsprice'] = $group['agio']*$goods['price']/100;
	}else{
		$goods['groupsprice'] =$goods['price'];
	}
	//商品多图
	$goods['images']=$goods['images']?json_decode($goods['images'],true):array();
	$goods['images'][]=$goods['image'];
	//查询该团的所有人
	$orders=pdo_getall('jy_aligroup_groups_order',array('uniacid'=>$_SESSION['uniacid'],'teamid'=>$teamid));
	if($orders){
		foreach($orders as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['member']['avatar']=$item['member']['avatar']?tomedia($item['member']['avatar']):'';
			if($item['mid']==$team['headsid']){
				$nickname=$item['member']['nickname'];
			}
			$item['paytime']=date('Y-m-d h:i',$item['paytime']);
		}
	}
	//团长的订单
	$tuan_first_order=pdo_get('jy_aligroup_groups_order',array('uniacid'=>$_SESSION['uniacid'],'teamid'=>$teamid,'mid'=>$team['headsid']));
	//限时时间
	$hours = $group['timeout'];
	$time = time();
	$date = date('Y-m-d H:i:s', $tuan_first_order['paytime']);
	$endtime = date('Y-m-d H:i:s', strtotime(' ' . $date . ' + ' . $hours . ' hour'));
	$date1 = date('Y-m-d H:i:s', $time);
	$lasttime2 = strtotime($endtime) - strtotime($date1);
	$tuan_first_order['endtime'] = strtotime(' ' . $date . ' + ' . $hours . ' hour');	
	$return['intDiff']=$tuan_first_order['paytime']+3600*$hours-time();
	if($team['status']==0){
		$team['statustitle']='快邀请好友一起来拼团吧！';
	}elseif($team['status']==1){
		$team['statustitle']='团购已成功！';
	}else{
		$team['statustitle']='团购已结束！';
	}
	$return['team']=$team;
	$return['orders']=$orders;
	$return['myorder']=$myorder;
	$return['goods']=$goods;
	$return['n']=$n;
	$return['lasttime2']=model('jy_weishop/model')->date($lasttime2);
	$return['tuan_first_order']=$tuan_first_order;
	$return['sharedata']=array(
			'title'=>$nickname.'-发起拼单啦！还差'.$n.'人就拼单成功了！',
			'desc'=>'拼着买，更便宜，'.$goods['title'].'-拼单特价啦！',
			'link'=>custom_url('jy_aligroup/teamdetail/detail/teamid='.$teamid.'&fromuser='.$_SESSION['uid'],true),
			'imgUrl'=>tomedia($goods['image'])
	);
	set_html('title','团详情');
	exi($return);
	
}
if($_RQ['op']=='getlist'){
	/*******main*******/
	//某活动下的所有的团
	$id = intval($_RQ['id']);
	$groupid = intval($_RQ['groupid']);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$return['params']=$params=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>0,'groupid'=>$groupid);
	$fields=array('id','uniacid','cid','groupid','goodsid','headsid','createtime','status');
	$teams=pdo_getslice('jy_aligroup_groups_team',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($teams as &$item){
		$item['num']=pdo_count('jy_aligroup_groups_order',array('uniacid'=>$_SESSION['uniacid'],'teamid'=>$item['id']));
	
	}
	
	$teams_count=count($teams);
	
	$return['teams']=$teams;
	exi($return);
	
}
if($_RQ['op']=='myteam'){
	/*******main*******/
	//我的团
	//先查询我的订单
	$orders=pdo_getall('jy_aligroup_groups_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'is_team'=>1));
	$teams=array();
	if($orders){
		foreach($orders as &$item){
			$orders['team']=pdo_get('jy_aligroup_groups_team',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['teamid']));
			
		}
		
	}
	$return['teams']=$teams;
	$return['teams_count']=$teams_count;
	exi($return);
	
}

?>